package com.sl.crawler.common.utils.yaml;

import com.sl.crawler.common.utils.encode.FileEncoding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * Created by shiling on 2017/6/17.
 */
public class YamlUtils {
    private static Logger LOG = LoggerFactory.getLogger(YamlUtils.class);

    public static final Object load(String filePath) {
        Yaml yaml = new Yaml();
        String fileCharacterEnding = FileEncoding.getFileCharacterEnding(filePath);
        try (InputStreamReader ir = new InputStreamReader(new FileInputStream(filePath), fileCharacterEnding)) {
            Object yamlObject = yaml.load(ir);
            return yamlObject;
        } catch (Exception e) {
            LOG.error("解析yaml失败!", e);
        }
        return null;
    }

    public static final <T> T loadObject(String filePath, Class<T> clazz) {
        return (T) load(filePath);
    }
}
